/*
 * 创建日期 2009-3-24
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.cpp.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;

import  org.fantasy.cpp.core.html.QueryHtml;
import  org.fantasy.cpp.core.html.util.TreeData;
import  org.fantasy.cpp.core.pojo.QueryConfig;
import  org.fantasy.cpp.core.pojo.QueryItem;
import  org.fantasy.cpp.core.pojo.QueryPage;
import  org.fantasy.cpp.core.service.PageService;
import  org.fantasy.cpp.core.service.QueryService;
import  org.fantasy.cpp.core.util.ConvertUtil;
import  org.fantasy.common.db.bean.Row;
import  org.fantasy.common.util.RequestUtil;

/**
 * 页面控制
 * 
 * @author: 王文成
 * @version: 1.0
 * @since 2011-3-24
 */
@Controller
public class QueryController extends AbstractController {

	private static final Log log = LogFactory.getLog(QueryController.class);

	public static final String PAGE_INFO = "page";

	public static final String PAGE_LIST = "list";

	@Autowired
	private PageService pageService;

	@Autowired
	private QueryService queryService;

	/**
	 * 查询页面初始化
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/query/main")
	public String main(Long pageId, ModelMap model) throws Exception {
		QueryPage page = pageService.getQueryPage(pageId);
		model.put(PAGE_INFO, page);
		return getView(page.getQueryTemplate().getMainPage());
	}

	/**
	 * 查询
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/query/list")
	public String list(Long pageId, ModelMap model, WebRequest request) throws Exception {
		QueryPage page = pageService.getQueryPage(pageId);
		QueryConfig config = page.getQueryConfig();
		Map<String, Object> qryMap = RequestUtil.getRequestMap(request);
		List<Row> list = null;
		if (page.isPage()) {
			list = queryService.qryPageList(config, qryMap);
		} else {
			list = queryService.qryList(config, qryMap);
		}
		model.put(PAGE_INFO, page);
		model.addAttribute(PAGE_LIST, list);
		return getView(page.getQueryTemplate().getResultPage());
	}
	
	/**
	 * 取得数据
	 * @param pageId
	 * @param itemId
	 * @param id
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/query/data")
	public String data(Long pageId, Long itemId, Long id,  ModelMap model, HttpServletRequest request) throws Exception {
		QueryPage page = pageService.getQueryPage(pageId);
		QueryItem item = page.getQueryItem(itemId);
		QueryConfig config = item.getQueryConfig();
		Map<String, Object> qryMap = RequestUtil.getRequestMap(request);
		// 如果ID为空，说明是第一次请求查询。需要给个起始节点ID，在查询中判断有没有startId
		if( id == null ){
			String value = item.getAttr(QueryHtml.START_ID);
			String startId = ConvertUtil.covert(request, value);
			qryMap.put("startId", startId);
		}
		List<Row> list = queryService.qryList(config, qryMap);
		TreeData convert = new TreeData(list);
		return ajax(convert.toJSON(true), model);
	}
	
	/**
	 * 查询总数
	 * @param pageId
	 * @param sql
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/query/count")
	public String count(Long pageId, String sql, ModelMap model) throws Exception {
		QueryPage page = pageService.getQueryPage(pageId);
		QueryConfig config = page.getQueryConfig();
		int count = 0;
		try {
			count = queryService.count(config, sql);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
		}
		return ajax("{count:" + count + "}", model);
	}
}